<template>
  <t-card :bordered="false">
    <!-- 自定义搜索 -->
    <custom-search ref="customSearchRef" v-slot="{ params }"  @submit="handleInitTable">
      <t-col :span="3">
        <t-form-item label="班组类型" name="jobType">
          <t-select
            v-model:value="params.jobType"
            :options="dictionaryStore.get('engineering_team_groups')"
            clearable
            filterable
            placeholder=""
          />
        </t-form-item>
      </t-col>
      <t-col :span="3">
        <t-form-item label="班组名称" name="name">
          <t-input v-model:value="params.name" clearable placeholder="" />
        </t-form-item>
      </t-col>
      <t-col :span="3">
        <t-form-item label="手机号码" name="phone">
          <t-input v-model:value="params.phone" clearable placeholder="" />
        </t-form-item>
      </t-col>
      
      <t-col :span="3">
        <t-form-item label="创建时间" name="createTime">
          <t-date-range-picker
            allow-input
            v-model:value="params.createTime"
            clearable
            enable-time-picker
            placeholder=" "
            style="width: 100%"
          />
        </t-form-item>
      </t-col>
    </custom-search>
    <!-- 自定义表格 -->
    <custom-table
      ref="customTableRef"
      row-key="id"
      :columns="columns"
      :api="engineeringTeamCRUD.page"
      column-controller-key="EngineeringTeamList"
    >
      <template #topContent>
        <div style="margin-bottom: var(--td-comp-margin-xxl);display: flex;justify-content: space-between;align-items: center;">
          <t-button
            v-permission="['system:engineering-team-users:create']"
            @click="handleOpenDialog('create', { 
              companyId:defaultCompany
              })"
          >
            新建班组
          </t-button>
          <t-radio-group v-model="defaultCompany" theme="button" @change="handleCompanyChange">
            <t-radio-button :value="_.id" v-for="_ in companyList">{{ _.name }}</t-radio-button>
          </t-radio-group>
        </div>
      </template>
    
   
    
      <template #jobType="{ row }">
        {{ dictionaryStore.analysis('engineering_team_groups', row.jobType) }}
      </template>
      <template #createTime="{ row }">
        {{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
      <template #operation="{ row }">
        <t-space break-line>
          <t-link
            v-permission="['system:engineering-team-users:query']"
            theme="primary"
            @click="handleOpenDialog('read', { id: row.id })"
          >
            详情
          </t-link>
          <t-link
            v-permission="['system:engineering-team-users:update']"
            theme="primary"
            @click="handleOpenDialog('update', { id: row.id })"
          >
            编辑
          </t-link>
          <t-popconfirm content="确认要删除该项吗？" @confirm="handleDelete({ id: row.id })">
            <t-link v-permission="['system:engineering-team-users:delete']" theme="danger"> 删除 </t-link>
          </t-popconfirm>
        </t-space>
      </template>
    </custom-table>
    <!-- 自定义对话框 -->
    <custom-dialog
      ref="customDialogRef"
      v-slot="{ formData, readonly }"
      :api-map="engineeringTeamCRUD"
      header="*班组"
      @request-success="handleDialogRequestSuccess"
    >
      <t-form-item label="班组类型" name="jobType" :rules="[{ required: true, message: '请选择班组类型' }]">
        <t-select
          v-model:value="formData.jobType"
          :options="dictionaryStore.get('engineering_team_groups')"
          placeholder="请选择班组类型"
          clearable
          filterable
          :readonly="readonly"
          @clear="formData.jobType = null"
        />
      </t-form-item>
      <t-form-item label="班组名称" name="name" :rules="[{ required: true, message: '请输入班组名称' }]">
        <t-input v-model:value="formData.name" placeholder="请输入班组名称" clearable :readonly="readonly" />
      </t-form-item>
      
      <t-form-item
        label="手机号码"
        name="phone"
        :rules="[
          { telnumber: true, message: '请输入正确的手机号码' },
          { required: true, message: '请输入手机号' }
        ]"
      >
        <t-input
          v-model:value="formData.phone"
          placeholder="请输入手机号码"
          clearable
          :readonly="readonly"
          maxlength="11"
        />
      </t-form-item>


    </custom-dialog>
  </t-card>
</template>


<script setup lang="ts">
import dayjs from 'dayjs';
import { MessagePlugin, TableCol } from 'tdesign-vue-next';
import { onMounted, ref, useTemplateRef } from 'vue';

import { engineeringTeamCRUD } from '@/api/config/common-config';
import { getCompanyList } from '@/api/system/dept';
import CustomDialog from '@/components/custom/dialog/index.vue';
import CustomSearch from '@/components/custom/search/index.vue';
import CustomTable from '@/components/custom/table/index.vue';
import { useDictionaryStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// 自定义搜索ref
const customSearchRef = useTemplateRef('customSearchRef');
// 自定义表格ref
const customTableRef = useTemplateRef('customTableRef');
// columns
const columns: TableCol[] = [
  { colKey: 'jobType', title: '班组类型' },
  { colKey: 'name', title: '班组名称' },
  { colKey: 'phone', title: '手机号码' },
  { colKey: 'createTime', title: '创建时间', width: 180 },
  { colKey: 'operation', title: '操作', width: 160, fixed: 'right' },
];
// 公司列表
const companyList = ref([]);
// 默认公司
const defaultCompany = ref()
// 处理获取公司列表
const handleGetCompanyList = () => {
  getCompanyList()
    .then((data) => {
      companyList.value = data;
      defaultCompany.value = data[0].id
      handleInitTable();
    })
    .catch(({ message }) => {
      MessagePlugin.error(message);
    });
};
// handleGetCompanyList();
// 处理初始化表格
const handleInitTable = () => {
  const params = { ...customSearchRef.value.params,companyId:defaultCompany.value };
  customTableRef.value.initTable(params);
};
// 处理刷新表格
const handleRefreshTable = () => {
  customTableRef.value.refreshTable();
};
onMounted(() => {
  handleGetCompanyList();
});
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};

// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  if (result.mode === 'create') handleInitTable();
  if (result.mode === 'update') handleRefreshTable();
};
// 处理删除
const handleDelete = (params: Record<string, any>) => {
  engineeringTeamCRUD
    .delete(params)
    .then(() => {
      MessagePlugin.success('删除成功');
      handleRefreshTable();
    })
    .catch(({ message }) => {
      MessagePlugin.error(message);
    });
};
// 公司切换
const handleCompanyChange = () => {
  handleInitTable();
};



</script>

<style lang="less" scoped>
:deep(.t-radio-group .t-radio-button) {
  border: none;
  border-radius: 18px !important;
  margin-left: 11px;
}
:deep(.t-radio-group .t-radio-button.t-is-checked) {
  background-color: #f2f3f5;
}
</style>
